#include "um_basic_types.h"
#include "umapi_libc.h"
#include "umapi_os.h"
#include "iniparser.h"
#include "serverConfig.h"
#include "umlog.h"

UM_S32 readConfig(ServerConfig *sc, UM_CHAR *configName)
{
	dictionary * pdict =  iniparser_load(configName);
	if(!pdict)
		return UM_FAILURE;
	sc->size = iniparser_getnsec(pdict);
	if(sc->size <= 0)
	{
		goto EXIT;
	}
	umapi_list_create(&sc->servers.params);
	UM_S32 i,j;
	UM_CHAR iniSection[40];
	for(i = 0;i<sc->size ; i++)
	{
		UM_CHAR *protol = iniparser_getsecname(pdict, i);
		ServerNodeParam *psnp = (ServerNodeParam*)umapi_malloc(sizeof(ServerNodeParam));
		if(!psnp)
		{
			LOGS("malloc server Param fail");
			break;
		}
		umapi_list_create(&psnp->head);
		umapi_list_add(&psnp->h0, &sc->servers.params);
		umapi_sprintf(psnp->protol, "%s", protol);
		UM_S32 len = iniparser_getsecnkeys(pdict, protol);
		UM_CHAR **keys = (UM_CHAR**)umapi_malloc(sizeof(UM_CHAR *) * len);
		if(!keys)
		{
			LOGS("malloc keys fail");
			umapi_free(psnp);
			break;
		}
		iniparser_getseckeys(pdict, protol, keys);
		for(j=0; j< len; j++)
		{
			KV *kv = umapi_malloc(sizeof(KV));
			if(!kv)
			{
				LOGS("malloc kv fail");
				break;
			}
			kv->key = umapi_malloc(umapi_strlen(keys[j]) +  1 - umapi_strlen(protol) -1);
			if(!kv->key)
			{
				LOGS("malloc kv.key fail");
				umapi_free(kv);
				break;
			}
			umapi_sprintf(kv->key, "%s", &keys[j][umapi_strlen(protol) + 1]);
			umapi_sprintf(iniSection,"%s:%s", protol, kv->key);			
			UM_CHAR *ptmp = iniparser_getstring(pdict,iniSection,"");
			kv->value = umapi_malloc(umapi_strlen(ptmp) + 1);
			if(!kv->value)
			{
				LOGS("malloc kv.value fail");
				umapi_free(kv);
				umapi_free(kv->key);
				break;
			}
			umapi_sprintf(kv->value, "%s", ptmp);
			umapi_list_add(&kv->h, &psnp->head);
		}
		umapi_free(keys);		
	}
EXIT:		
	iniparser_freedict(pdict);
	return UM_SUCCESS;
}

UM_S32 freeConfig(ServerConfig *sc)
{
	if(!sc)
		return UM_SUCCESS;
	struct umapi_list_head *pos ,*n, *pos1, *n1;
	umapi_list_for_each_safe(pos,n, &sc->servers.params)
	{
		ServerNodeParam *psnp = umapi_list_entry(pos,ServerNodeParam,h0);
		umapi_list_for_each_safe(pos1,n1, &psnp->head)
		{
			KV *kv = umapi_list_entry(pos1, KV ,h);
			umapi_list_del(&kv->h);
			if(kv->key)
				umapi_free(kv->key);
			if(kv->value)
				umapi_free(kv->value);
			umapi_free(kv);
		}
		umapi_list_del(&psnp->h0);
	}
	return UM_SUCCESS;
}


S_SYSTEM_DATA gSystemData = {0};



UM_VOID setAppMediaPathPrefix(UM_CHAR *p)
{
	umapi_sprintf(gSystemData.prefixName ,"%s", p);
}
#if 0
UM_CHAR * getAppMediaPathPrefix()
{
	return gSystemData.prefixName;
}

UM_CHAR *getAppVoHandleHost()
{
	return gSystemData.voHost;
}
UM_CHAR *getAppAoHandleHost()
{
	return gSystemData.aoHost;
}
UM_U16 getAppVoHandlePort()
{
	return gSystemData.voPort;
}
UM_U16 getAppAoHandlePort()
{
	return gSystemData.aoPort;
}
UM_CHAR *getAppVoHandlePath()
{
	return gSystemData.vohandlePath;
}
UM_CHAR *getAppAoHandlePath()
{
	return gSystemData.aohandlePath;
}
UM_CHAR *getAppPublicHost()
{
	return gSystemData.appPulicHost;
}
#endif
UM_VOID setAppPublicHost(UM_CHAR *p)
{
	umapi_sprintf(gSystemData.appPulicHost ,"%s", p);
}
UM_VOID setVoHandle(UM_CHAR *p)
{
	decUrl(gSystemData.voHost, &gSystemData.voPort, gSystemData.vohandlePath, p);
}
UM_VOID setAoHandle(UM_CHAR *p)
{
	decUrl(gSystemData.aoHost, &gSystemData.aoPort, gSystemData.aohandlePath, p);
}
UM_VOID setHlsHandle(UM_CHAR *p)
{
	decUrl(gSystemData.hlsHost, &gSystemData.hlsPort, gSystemData.hlshandlePath, p);
}

